/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package common;

import java.sql.Timestamp;
import java.util.Calendar;

/**
 *
 * @author HoangHai
 */
public class DateUtility {

    public static Timestamp getTimestampNow(boolean timeReset) {
        Timestamp now = new Timestamp(System.currentTimeMillis());
        if (timeReset) {
            now = resetTime(now);
        }
        return now;
    }

    public static Timestamp addDate(Timestamp baseDate, int addDay) {
        if (addDay < 0) {
            return baseDate;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(baseDate);
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) + addDay);
        Timestamp reDate = new Timestamp(cal.getTime().getTime());
        return reDate;

    }

    public static Timestamp addMonth(Timestamp baseDate, int addMonth) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(baseDate);
        cal.add(Calendar.MONTH, addMonth);
        Timestamp reDate = new Timestamp(cal.getTime().getTime());
        return reDate;

    }

    public static Timestamp addYear(Timestamp baseDate, int addYear) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(baseDate);
        cal.add(Calendar.YEAR, addYear);
        Timestamp reDate = new Timestamp(cal.getTime().getTime());
        return reDate;

    }

    public static Timestamp subDate(Timestamp baseDate, int subDay) {
        if (subDay < 0) {
            return baseDate;
        }
        Calendar cal = Calendar.getInstance();
        cal.setTime(baseDate);
        cal.set(Calendar.DATE, cal.get(Calendar.DATE) - subDay);
        Timestamp reDate = new Timestamp(cal.getTime().getTime());
        return reDate;

    }

    public static Timestamp subMonth(Timestamp baseDate, int subMonth) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(baseDate);
        cal.set(Calendar.MONTH, cal.get(Calendar.MONTH) - subMonth);
        Timestamp reDate = new Timestamp(cal.getTime().getTime());
        return reDate;

    }

    public static Timestamp subYear(Timestamp baseDate, int subYear) {

        Calendar cal = Calendar.getInstance();
        cal.setTime(baseDate);
        cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) - subYear);
        Timestamp reDate = new Timestamp(cal.getTime().getTime());
        return reDate;

    }

    public static int getBetweenDayCount(Timestamp pFromDay, Timestamp pFoDay) {


        final long aDay = 24 * 60 * 60 * 1000;

        if (pFromDay == null || pFoDay == null) {
            return -1;
        }

        Timestamp fromDay = resetTime(pFromDay);
        Timestamp toDay = resetTime(pFoDay);

        long sabun = toDay.getTime() - fromDay.getTime();

        long dayCnt = sabun / aDay;
        if (dayCnt < 0) {
            return -1;
        }

        return (int) dayCnt + 1;

    }

    public static int compareTo(Timestamp t1, Timestamp t2) {

        Timestamp tm1 = resetTime(t1);
        Timestamp tm2 = resetTime(t2);
        return tm1.compareTo(tm2);

    }

    public static boolean compareTo(Timestamp t1, Timestamp t2, Integer subNum) {

        Timestamp tm1 = resetTime(t1);
        Timestamp tm2 = resetTime(t2);

        if (subNum != null) {
            Timestamp subTime = subYear(tm1, subNum);
            int ret = compareTo(tm2, subTime);
            if (ret >= 0) {
                return true;
            }
            return false;
        }
        return false;
    }

    public static Timestamp resetTime(Timestamp time) {
        if (time == null) {
            return null;
        }
        String str = time.toString().substring(0, 10);
        Timestamp newTime = Timestamp.valueOf(str + " 00:00:00");
        return newTime;
    }
}
